package com.zky.seckillzky.seckill.controller;

import com.alipay.api.AlipayApiException;
import com.zky.seckillzky.seckill.entity.OrderEntity;
import com.zky.seckillzky.seckill.entity.UserEntity;
import com.zky.seckillzky.seckill.service.impl.OrderServiceImpl;
import com.zky.seckillzky.seckill.util.AlipayTemplate;
import com.zky.seckillzky.seckill.vo.PayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/alipay")
public class payController {

    @Autowired
    private OrderServiceImpl OrderServiceImpl;

    @Autowired
    private AlipayTemplate alipayTemplate;

    @Autowired
    private OrderServiceImpl orderService;



    @ResponseBody
    @GetMapping(value = "/payOrder",produces = "text/html")
    public String payOrder(UserEntity userEntity, @RequestParam("orderSn") String orderSn) throws AlipayApiException {
//        PayVo payVo = new PayVo();
//        payVo.setBody();//订单备注
//        payVo.setOut_trade_no();//订单号
//        payVo.setSubject();//标题
//        payVo.setTotal_amount();//订单金额
        PayVo payVo =  OrderServiceImpl.getOrderPay(orderSn,userEntity);
        //返回的是一个页面，将此页面直接交给浏览器就行
        String pay = alipayTemplate.pay(payVo);

        System.out.println(pay);

        return pay;
    }


    @GetMapping("/memberOrder.html")
    public String memberOrder( Model model, UserEntity userEntity){
        OrderEntity orderInfo = orderService.OrderUserId(userEntity.getUserId());
        model.addAttribute("orderInfo",orderInfo);
        return "memberOrder";
    }





}
